<?php
class JobprofilesController extends AppController {

	var $name = 'Jobprofiles';
         function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
            $this->set('active',3);

        }
        
	function index() {
		$this->Jobprofile->recursive = 0;
		$this->set('jobprofiles', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid jobprofile', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('jobprofile', $this->Jobprofile->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Jobprofile->create();
			if ($this->Jobprofile->save($this->data)) {
				$this->Session->setFlash(__('The jobprofile has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The jobprofile could not be saved. Please, try again.', true));
			}
		}
		$securitygroups = $this->Jobprofile->Securitygroup->find('list');
		$tools = $this->Jobprofile->Tool->find('list');
		$users = $this->Jobprofile->User->find('list');
		$this->set(compact('securitygroups', 'tools', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid jobprofile', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Jobprofile->save($this->data)) {
				$this->Session->setFlash(__('The jobprofile has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The jobprofile could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Jobprofile->read(null, $id);
		}
		$securitygroups = $this->Jobprofile->Securitygroup->find('list');
		$tools = $this->Jobprofile->Tool->find('list');
		$users = $this->Jobprofile->User->find('list');
		$this->set(compact('securitygroups', 'tools', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for jobprofile', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Jobprofile->delete($id)) {
			$this->Session->setFlash(__('Jobprofile deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Jobprofile was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
        function joblist($jid=null){
            $this->layout='ajax';
            if ($jid){
                $invalidities=$this->Jobprofile->find('first',array(
                    'conditions'=>array(
                        'id'=>$jid
                    ),
                    'fields'=>array('invalidities')
                ));
                $invalidities=$invalidities['Jobprofile']['invalidities'];
                if ($invalidities!='all'){
                    $invalidities=split(';', $invalidities);
                    $this->set('joblists',$this->Jobprofile->find('list',array(
                    'conditions'=>array(
                       'NOT'=>array('id'=>$invalidities)
                    )
                )));
                }else{
                    $this->set('joblists',$this->Jobprofile->find('list',array(
                    'conditions'=>array(
                       'id'=>array('1','2')
                    )
                )));
                }
                $this->set('invalidities',$invalidities);
                
                
            }else{ 
               $this->set('joblists',$this->Jobprofile->find('list'));
            }
        }
        
        function checkCompatibility($jid,$jcid){
            $this->layout='ajax';
            if ($jid && $jcid){
                $invalidities=$this->Jobprofile->find('first',array(
                    'conditions'=>array(
                        'id'=>$jid
                    ),
                    'fields'=>array('invalidities')
                ));
                $invalidities=$invalidities['Jobprofile']['invalidities'];
                //$jcarray=array($jcid);
                if ($invalidities!='all'){
                    $invalidities=split(';', $invalidities);
                    if (in_array ($jcid, $invalidities)){
                        $this->set('compatibility',false);
                    }else{
                        $this->set('compatibility',true);
                    }
                }else{
                    if (in_array($jcid, array('1','2'))){
                        $this->set('compatibility',true);
                    }else{
                        $this->set('compatibility',false);
                    }
                    
                }
            }
        }
}
?>